package com.wumart.g6cm.web;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.wumart.scaffold.web.ScaffoldApplication;
import com.wumart.scaffold.web.base.generator.GeneratorDataSourceConfig;
import com.wumart.scaffold.web.base.generator.GeneratorGlobalConfig;
import com.wumart.scaffold.web.base.generator.GeneratorPackageConfig;
import com.wumart.scaffold.web.base.generator.GeneratorStrategyConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangwc@wumart.com
 * @version v1.0.0
 * @date 2021/1/18 19:29
 */
@SpringBootTest(classes = ScaffoldApplication.class)
public class MybatisPlusG6Generator2 {

    @Autowired
    private GeneratorDataSourceConfig dsc;
    @Autowired
    private GeneratorGlobalConfig gc;
    @Autowired
    private GeneratorPackageConfig pc;
    @Autowired
    private GeneratorStrategyConfig strategy;
    @Test
    void contextLoads() {

        // Step1：代码生成器
        AutoGenerator  mpg = new AutoGenerator();

        mpg.setGlobalConfig(gc);

        mpg.setDataSource(dsc);

        mpg.setPackageInfo(pc);

        // 填写代码生成的目录(需要修改)
        String projectPath = "D:\\work\\common-scaffold\\scaffold-web";
        String templatePath = "/templates/mapper.xml.vm";
        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return projectPath + "/src/main/resources/mapper/g6/"
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));
        mpg.setTemplateEngine(new VelocityTemplateEngine());
        mpg.setStrategy(strategy);

        // Step6：执行代码生成操作
        mpg.execute();
    }
}
